/**
 * Created by Lwang on 2017/3/5.
 * 所有文章
 *
 */

import {square, ajax} from './resize.js';
import {way} from "./common.js";
import $ from "jquery";
require("./layer.js");
// var Handlebars = require("./handlebars.js");
square(document, window);

$(function () {
    var bool = true;
    way._login(() => {
        if (location.href.indexOf("newest") == -1) {
            // 所有文章
            $.ajax({
                url: ajax.listCourse + "?size=50&page=0&type=view",
                xhrFields: {withCredentials: true},
                type: "GET",
                success: (res) => {
                    var content = res.content;
                    if (content.length > 0) {
                        $("#j-num").html("").html(res.totalElements);
                        modleHandle(content, "#template-contain");

                        //    删除按钮
                        $("#toDelete").click(function () {
                            $(".contenting").css("width", "13rem");
                            $(".ml-content").css("width", "8.3rem");
                            $(".deleteing").show();
                            $("#toDelete").hide();
                            $("#toComplete").show();
                        });
                        $("#toComplete").click(function () {
                            $(".contenting").css("width", "15rem");
                            $(".ml-content").css("width", "10.375rem");
                            $(".deleteing").hide();
                            $("#toDelete").show();
                            $("#toComplete").hide();
                        });

                        $(document).on("touchend", ".deleteing", function () {
                            if (bool) {
                                bool = false;
                                layer.open({
                                    type: 2,
                                    message: "正在加载"
                                });
                                var id = $(this).parent().find(".contentId").val(),
                                    _index = $(this).parent().index();

                                $.ajax({
                                    url: ajax.delCourse(id),
                                    type: "POST",
                                    xhrFields: {withCredentials: true},
                                    success: (ofres) => {
                                        layer.closeAll();
                                        if (!ofres.code) {
                                            layer.open({
                                                type: 1,
                                                content: "删除成功!",
                                                time: 1
                                            });
                                            content.splice(_index, 1);
                                            content.forEach(function (obj, index) {
                                                if (obj.id == id) {
                                                    content.splice(index, 1);
                                                }
                                            });

                                            modleHandle(content, "#template-contain", function () {
                                                bool = true;
                                                //    删除后返回删除状态
                                                $(".contenting").css("width", "13rem");
                                                $(".ml-content").css("width", "8.3rem");
                                                $(".deleteing").show();
                                            });


                                        } else {
                                            layer.open({
                                                type: 1,
                                                content: ofres.message
                                            });
                                        }

                                    }
                                })
                            }

                        })
                    } else {
                        $("#template-contain").hide();
                        $(".no-data").show();
                    }
                }
            });
        }

        if (location.href.indexOf("newest") > -1) {
            //最新
            $.ajax({
                url: ajax.listCourse + "?size=15&page=0&type=use",
                xhrFields: {withCredentials: true},
                type: "GET",
                success: (res) => {
                    var content = res.content;
                    if (content.length > 0) {
                        modleHandle(content, "#newestArticle");
                    } else {
                        $("#newestArticle").hide();
                        $(".no-data").show();
                    }
                }
            });
        }

    });

    //监听滚轮
    // $(window).scroll(function () {
    //     var scroll = $(document).scrollTop();
    //     if (scroll == 0) {
    //         $(".m-title").css({
    //             position: "static",
    //         });
    //         $(".aa-title").css({
    //             position: "static",
    //         });
    //         $(".m-list").css({
    //             marginTop: 0
    //         })
    //     } else {
    //         $(".m-title").css({
    //             position: "fixed",
    //             top: 0,
    //             left: "0.5rem",
    //             zIndex:9999
    //         });
    //         $(".aa-title").css({
    //             position: "fixed",
    //             top: 0,
    //             left: "0.5rem",
    //             zIndex:9999
    //         });
    //         $(".m-list").css({
    //             marginTop: "2rem"
    //         })
    //     }
    // });

    //模板函数
    function modleHandle(allData, selector, callback) {
        if (allData.length == 0) {
            $(selector).hide();
            $(".no-data").show();
        }
        $(selector).html("");
        $.each(allData, function (index, obj) {
            var newdom = $(dom);
            var time = new Date(allData[index].createTime);

            $(".contenting", newdom).attr("href", "/article-edit.html?courseid=" + allData[index].id);
            $(".egImg", newdom).attr("src", allData[index].firstImageContent ? allData[index].firstImageContent.content : "images/edit-img.jpg");
            $(".ofTitle", newdom).html("").html(allData[index].title);
            $(".mlc-date", newdom).html("").html((time.getMonth() + 1) + "-" + time.getDate() + " " + time.getHours() + ":" + time.getSeconds());
            $(".mlc-num", newdom).html("").html(allData[index].count);
            $(".contentId", newdom).val(allData[index].id);
            $(".mlc-enable",newdom).html("").html(allData[index].status == "AUDITING" ? "未发布" : "已发布");
            $(selector).append(newdom);
        });
        way._typeOf(callback) == "[object function]" && callback(allData);

    }

    //字符串
    var dom = '<li>' +
        '<a href="" class="contenting">' +
        '<div class="ml-img">' +
        '<img src="" alt="示例图" class="egImg">' +
        '</div>' +
        '<div class="ml-content">' +
        '<h3 class="ofTitle"></h3>' +
        '<div class="mlc-right">' +
        '<span class="mlc-date"></span>' +
        '<span class="mlc-num"></span>' +
        '<span class="mlc-enable"></span>' +
        '</div>' +
        '</div>' +
        '</a>' +
        '<a class="deleteing">删除</a>' +
        '<input type="hidden"  class="contentId">' +
        '</li>'

});


